﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmDinhKhoanMuaHang : Form
    {
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private ButToanMgt buttoanMgt = new ButToanMgt();        
        private HangHoaManagement hanghoaMgt = new HangHoaManagement();
        public long tien = 0;
        public long tongtien = 0;
        private MuaHang muahang = new MuaHang();
        public MuaHang MuaHang
        {
            set { this.muahang = value; }
            get { return this.muahang; }
        }

        public FrmDinhKhoanMuaHang()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
            {
                if ((dgvDinhKhoan.Rows[i].Cells[0].FormattedValue.ToString() == "" || dgvDinhKhoan.Rows[i].Cells[1].FormattedValue.ToString() == ""))
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin trong danh sách !","Thông báo");
                    return;
                }
            }

            if (dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString() != "331")
            {
                MessageBox.Show("Tài khoản gốc phải là 331 !", "Thông báo");
                return;
            }
            if (dgvDinhKhoan.Rows[0].Cells[2].FormattedValue.ToString() == "Nợ")
            {
                MessageBox.Show("Định khoản " + dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString() + " phải chọn \"Có\" !!!", "Thông báo");
                return;
            }

            for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
            {
                if (dgvDinhKhoan.Rows[i].Cells[2].FormattedValue.ToString() == "Có" || dgvDinhKhoan.Rows[i].Cells[2].FormattedValue.ToString() == "")
                {
                    MessageBox.Show("Các tài khoản khác " + dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString() + " phải chọn \"Nợ\"");
                    return;
                }
            }

            if (tien == tongtien)
            {
                ResultObj resultObj = new ResultObj();

                if (dgvDinhKhoan.Rows[0].Cells[2].FormattedValue.ToString() == "Nợ")
                {
                    MessageBox.Show("Định khoản 331 phải chọn có !!!", "Thông báo");
                    return;
                }

                //check 331= "Co" != 1521 "No" and != 1331 "No"
                for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
                {
                    if (dgvDinhKhoan.Rows[i].Cells[2].FormattedValue.ToString() == "Có")
                    {
                        MessageBox.Show("Các định khoản khác 331 phải chọn nợ !!!", "Thông báo");
                        return;
                    }
                }

                EditOrRemoveDinhKhoan(muahang.MaSo);

                for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
                {
                    if (dgvDinhKhoan.Rows[i].Cells[4].FormattedValue.ToString() != "0")
                    {
                        ButToan buttoan = new ButToan();
                        buttoan.NgayThang = dtpNgay.Value;
                        buttoan.ChiNhanh = txtChiNhanh.Text;
                        buttoan.MaSoKhachHang = txtMSKH.Text;
                        buttoan.DienGiai = txtNoiDung.Text;
                        buttoan.SoChungTu = txtSoChungTu.Text;
                        buttoan.TKGoc = dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString();
                        buttoan.TKDoiUng = dgvDinhKhoan.Rows[i].Cells[1].FormattedValue.ToString();
                        buttoan.GhiNo = 0;
                        buttoan.GhiCo = Convert.ToInt64(dgvDinhKhoan.Rows[i].Cells[4].Value.ToString());
                        resultObj = buttoanMgt.SaveNewButToan(buttoan);

                        buttoan = new ButToan();
                        buttoan.NgayThang = dtpNgay.Value;
                        buttoan.ChiNhanh = txtChiNhanh.Text;
                        buttoan.MaSoKhachHang = txtMSKH.Text;
                        buttoan.DienGiai = txtNoiDung.Text;
                        buttoan.SoChungTu = txtSoChungTu.Text;
                        buttoan.TKGoc = dgvDinhKhoan.Rows[i].Cells[1].FormattedValue.ToString();
                        buttoan.TKDoiUng = dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString();
                        buttoan.GhiNo = Convert.ToInt64(dgvDinhKhoan.Rows[i].Cells[4].Value.ToString());
                        buttoan.GhiCo = 0;
                        resultObj = buttoanMgt.SaveNewButToan(buttoan);
                    }

                }

                if (resultObj.ReturnCode > 0)
                {
                    MessageBox.Show("Lưu thành công", "Thông báo");
                    this.Close();

                }
                else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                else
                    MessageBox.Show("Thất bại");
            }
            else
            {
                MessageBox.Show("Số tiền định khoảng không đúng", "Thông báo");
            }
        }

        private void FrmDinhKhoanMuaHang_Load(object sender, EventArgs e)
        {
            ResultObj resultobj = new ResultObj();

            resultobj = hanghoaMgt.GetAllHangHoa();
            List<HangHoa> listHangHoa = (List<HangHoa>)resultobj.Result;           

            txtSoChungTu.Text = muahang.MaSo;
            txtMSKH.Text = muahang.NhaCungCap;
            txtChiNhanh.Text = muahang.DiaChi;
            txtMSKH.ReadOnly = true;
            txtSoChungTu.ReadOnly = true;
            dtpNgay.Enabled = false;
            txtChiNhanh.ReadOnly = true;

            
            List<ChiNhanhCongTy> lstChiNhanhCty = (from cn in GetAllChiNhanhCty()
                                                    where cn.Id == muahang.IdChiNhanhCongTy
                                                    select cn).ToList();

            if (lstChiNhanhCty.Count > 0)
                txtChiNhanh.Text = lstChiNhanhCty[0].TenChiNhanh;

            cboSoTK.DataSource = GetAllTaiKhoanKeToan();
            cboSoTK.DisplayMember = "MaSoTK";
            cboSoTK.ValueMember = "Id";

            dgvDinhKhoan.Columns["colSoTien"].ReadOnly = false;

            DataGridViewRow row = new DataGridViewRow();

            DataGridViewCell cell = new DataGridViewTextBoxCell();
            cell.Value = "";
            row.Cells.Add(cell);

            DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
            List<TaiKhoanKeToan> lstTaiKhoanKeToan = GetAllTaiKhoanKeToan();
            cboCell.DataSource = lstTaiKhoanKeToan;
            cboCell.DisplayMember = "MaSoTK";
            cboCell.ValueMember = "Id";

            TaiKhoanKeToan tkkt = new TaiKhoanKeToan();
            for (int i = 0; i < lstTaiKhoanKeToan.Count; i++)
                if (lstTaiKhoanKeToan[i].MaSoTK == "331")
                    tkkt = lstTaiKhoanKeToan[i];
            cboCell.Value = tkkt.Id;
            row.Cells.Add(cboCell);

            cboCell = new DataGridViewComboBoxCell();
            cboCell.Items.Add("Nợ");
            cboCell.Items.Add("Có");
            cboCell.Value = "Có";
            row.Cells.Add(cboCell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = tkkt.TenTK;
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = 0;
            row.Cells.Add(cell);

            dgvDinhKhoan.Rows.Add(row);

            for (int i = 0; i < this.muahang.listCTMuaHang.Count; i++)
            {
                row = new DataGridViewRow();

                cell = new DataGridViewTextBoxCell();
                cell.Value = this.muahang.listCTMuaHang[i].TenHang;
                row.Cells.Add(cell);

                cboCell = new DataGridViewComboBoxCell();
                cboCell.DataSource = lstTaiKhoanKeToan;
                cboCell.DisplayMember = "MaSoTK";
                cboCell.ValueMember = "Id";
                cboCell.Value = "";
                row.Cells.Add(cboCell);

                cboCell = new DataGridViewComboBoxCell();
                cboCell.Items.Add("Nợ");
                cboCell.Items.Add("Có");
                cboCell.Value = "";
                row.Cells.Add(cboCell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = "";
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = this.muahang.listCTMuaHang[i].SoLuong * this.muahang.listCTMuaHang[i].DonGia;
                tongtien += this.muahang.listCTMuaHang[i].SoLuong * this.muahang.listCTMuaHang[i].DonGia;
                row.Cells.Add(cell);

                dgvDinhKhoan.Rows.Add(row);
            }
        }

        private void dgvDinhKhoan_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.RowIndex > -1)
            {
                ResultObj result = new ResultObj();
                List<TaiKhoanKeToan> lstTaiKhoanKeToan = (from tkkt in GetAllTaiKhoanKeToan()
                                                          where tkkt.Id == Convert.ToInt32(dgvDinhKhoan.Rows[e.RowIndex].Cells[1].Value.ToString())
                                                          select tkkt).ToList();
                string tenTK = lstTaiKhoanKeToan[0].TenTK;

                dgvDinhKhoan.Rows[e.RowIndex].Cells[3].Value = tenTK;
                int soDinhKhoan = Convert.ToInt32(dgvDinhKhoan.Rows[e.RowIndex].Cells[1].FormattedValue.ToString());

                switch (soDinhKhoan)
                {
                    case 1331:
                        if (this.muahang.TienVAT != 0)
                            dgvDinhKhoan.Rows[e.RowIndex].Cells[4].Value = this.muahang.TienVAT.ToString();
                        else
                            dgvDinhKhoan.Rows[e.RowIndex].Cells[4].Value = 0;
                        break;
                    default:
                        if (dgvDinhKhoan.Rows[e.RowIndex].Cells[4].Value == null)
                            dgvDinhKhoan.Rows[e.RowIndex].Cells[4].Value = 0;
                        break;
                }
            }

            if (e.ColumnIndex == 4 && e.RowIndex > -1 && dgvDinhKhoan.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() != "331")
            {
                if (dgvDinhKhoan.Rows[e.RowIndex].Cells[4].Value == null)
                    dgvDinhKhoan.Rows[e.RowIndex].Cells[4].Value = 0;
            }

            long tongBaBaMot = 0;
            for (int j = 1; j < dgvDinhKhoan.Rows.Count - 1; j++)
            {
                if (dgvDinhKhoan.Rows[j].Cells[4].Value != null)
                {
                    tongBaBaMot += Convert.ToInt64(dgvDinhKhoan.Rows[j].Cells[4].Value.ToString());
                }
            }
            tien = tongBaBaMot;
            dgvDinhKhoan.Rows[0].Cells[4].Value = tongBaBaMot;
        }

        private void dgvDinhKhoan_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMSKH_TextChanged(object sender, EventArgs e)
        {
            this.Focus();
        }

        public void EditOrRemoveDinhKhoan(string MaSo)
        {
            ResultObj resultObj = new ResultObj();

            List<ButToan> lst = (from p in GetAllButToan()
                                 where p.SoChungTu == MaSo
                                 select p).ToList();

            if (lst.Count > 0)
            {
                foreach (ButToan b in lst)
                {
                    b.IsEdited = 1;
                    resultObj = buttoanMgt.UpdateButToan(b);
                }
            }
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCty()
        {
            ResultObj resultObj = new ResultObj();

            List<ChiNhanhCongTy> lstChiNhanhCongTy = new List<ChiNhanhCongTy>();

            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            lstChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return lstChiNhanhCongTy;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvDinhKhoan.CurrentRow != null)
            {
                if (dgvDinhKhoan.CurrentRow.Index != (dgvDinhKhoan.Rows.Count - 1))
                {
                    dgvDinhKhoan.Rows.RemoveAt(dgvDinhKhoan.CurrentRow.Index);
                    long sotien = Convert.ToInt64(dgvDinhKhoan.Rows[dgvDinhKhoan.CurrentRow.Index].Cells[3].Value.ToString());
                    for (int i = 0; i < dgvDinhKhoan.Rows.Count; i++)
                    {
                        if (dgvDinhKhoan.Rows[i].Cells[0].FormattedValue.ToString() == "331")
                        {
                            long sotien331 = Convert.ToInt64(dgvDinhKhoan.Rows[i].Cells[3].Value.ToString());
                            long result = sotien331 - sotien;
                            this.tongtien = result;
                            dgvDinhKhoan.Rows[i].Cells[3].Value = result;
                            break;
                        }

                    }

                }
            }
        }

        private void dgvDinhKhoan_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvDinhKhoan.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvDinhKhoan.ClearSelection();
                    dgvDinhKhoan.CurrentCell = dgvDinhKhoan.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvDinhKhoan.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }
    }
}
